package com.mjk.common.core.jdbc.service.plus.provider.impl.wrap;



import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.core.jdbc.service.plus.constns.PlusConstns;
import com.mjk.common.core.jdbc.service.plus.provider.BeeDataSourceType;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeComplexQueryWrapper;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.core.jdbc.util.TableUtils;
import com.mjk.common.util.MapUtils;
import com.mjk.common.util.StrUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Sql语句提供类 用于解析querywrapper
 */
public class WrapSQLProvider<T extends BaseEntity> {

    public String excuteSql(Map<String, Object> map) {
        return MapUtils.getString(map, "sql");
    }

    public String complexSelectByWrapp(Map<String, Object> map) {
        BeeComplexQueryWrapper example = (BeeComplexQueryWrapper) map.get("wrap");
        String driver = TableUtils.getDriverName(example.getInstance());
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).complexSelectByWrapp(map);
    }

    public String complexSelectByWrappWithPage(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder(" select * from (");
        sb.append(complexSelectByWrapp(map));
        sb.append(") as tmp");
        return sb.toString();
    }

    public String countByExample(Map<String, Object> map) {
        BeeQueryWrapper example = (BeeQueryWrapper) map.get("wrap");
        String driver = TableUtils.getDriverName(example.getInstance());
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).countByExample(map);
    }

    public String deleteByExample(Map<String, Object> map) {
        BeeQueryWrapper example = (BeeQueryWrapper) map.get("wrap");
        String driver = TableUtils.getDriverName(example.getInstance());
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).deleteByExample(map);
    }

    public String deleteByPrimaryKey(T t) {
        return TableUtils.getHandler(t).deleteByPrimaryKey(t);
    }

    public String insert(Map<String, Object> map) {
        BaseEntity app = (BaseEntity) map.get("item");
        String driver = TableUtils.getDriverName(app);
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).insert(map);
    }

    public String selectByExample(Map<String, Object> map) {
        BeeQueryWrapper example = (BeeQueryWrapper) map.get("wrap");
        String driver = TableUtils.getDriverName(example.getInstance());
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).selectByExample(map);
    }

    public String selectByPrimaryKey(Map<String, Object> map) {
        BaseEntity app = (BaseEntity) map.get("dto");
        String driver = TableUtils.getDriverName(app);
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).selectByPrimaryKey(map);
    }

    public String updateByPrimaryKeySelective(Map<String, Object> map) {
        BaseEntity t = (BaseEntity) map.get("dto");
        String driver = TableUtils.getDriverName(t);
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).updateByPrimaryKeySelective(map);
    }

    public String updateByExampleSelective(Map<String, Object> map) {
        BeeQueryWrapper wrapper = (BeeQueryWrapper) map.get("wrap");
        String driver = TableUtils.getDriverName(wrapper.getInstance());
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).updateByExampleSelective(map);
    }

    public String deleteBaseList(Map<String, Object> map) {
        List list = (List) map.get("list");
        BaseEntity entity = (BaseEntity) list.get(0);
        String driver = TableUtils.getDriverName(entity);
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).deleteBaseList(map);
    }

    public String updateBaseList(Map<String, Object> para) {
        List list = (List) para.get("list");
        BaseEntity entity = (BaseEntity) list.get(0);
        String driver = TableUtils.getDriverName(entity);
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).updateBaseList(para);
    }

    public String insertBaseList(Map<String, Object> para) {
        List list = (List) para.get("list");
        StrUtils.notNull(list, "list is null");
        BaseEntity entity = (BaseEntity) list.get(0);
        String driver = TableUtils.getDriverName(entity);
        return Objects.requireNonNull(BeeDataSourceType.getHandler(driver, PlusConstns.WRAP)).insertBaseList(para);
    }
}
